package com.sphd.restaurantsystem.model;


import android.util.Log;

import com.android.volley.VolleyError;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.CouponInfo;
import com.sphd.restaurantsystem.interfaces.VolleyJsonObjectCallBack;
import com.sphd.restaurantsystem.interfaces.VolleyStringCallBack;
import com.sphd.restaurantsystem.presenter.SureOrderPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
* Created by Hu on 2016/12/05
*/

public class SureOrderModelImpl implements SureOrderModel, VolleyJsonObjectCallBack {
    SureOrderPresenterImpl sureOrderPresenter;
    @Override
    public void sendUrl(String url, SureOrderPresenterImpl sureOrderPresenter) {
        this.sureOrderPresenter=sureOrderPresenter;
        volleyUtil.setJsonObjectRequest(url,this);
    }

    @Override
    public void sendMap(Map<String, String> map, SureOrderPresenterImpl sureOrderPresenter) {
        this.sureOrderPresenter=sureOrderPresenter;
        volleyUtil.setStringRequest(RestaurantUrl.Httptitle + RestaurantUrl.CLIENTORDER, new VolleyStringCallBack() {
            @Override
            public void onResponse(String response) {
                sureOrderPresenter.sendOrderRes(response);

            }

            @Override
            public void onErrorResponse(VolleyError error) {
                sureOrderPresenter.sendError(error);
                Log.e("error",error.toString());
            }
        }, map);

    }

    @Override
    public void onResponse(JSONObject response) {
        try {
            List<CouponInfo> cardInfos = JsonUtil.getListFromString(response.getString("cards"),CouponInfo.class);
            double account=response.getDouble("account");
            String gmtTime = response.getString("currentTime");
            sureOrderPresenter.sendCards(cardInfos);
            sureOrderPresenter.sendMoney(account,gmtTime);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        sureOrderPresenter.sendError(error);
    }
}